package com.itxzh.logindemo.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.itxzh.logindemo.entities.Users;
import com.itxzh.logindemo.service.UsersService;
import com.itxzh.logindemo.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description
 *
 * @Author ldt
 * @Date 2022/5/20 14:34
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/users")
@Slf4j
public class UsersController {

    @Autowired
    UsersService usersService;

    @RequestMapping("/getList")
    public Map<String, Object> getList(Users users) {
        List<Users> list = usersService.getList(users);
        Map<String, Object> ret = new HashMap<>();
        ret.put("status", 1);
        ret.put("msg", "请求成功");
        ret.put("data", list);
        return ret;
    }

    @RequestMapping("/getUser")
    public Map<String, Object> getUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        DecodedJWT dj = JwtUtil.verify(token);
        String id = dj.getClaim("id").asString();
        String name = dj.getClaim("name").asString();

        log.info("id=" +  id);
        log.info("name=" +  name);

        Map<String, Object> ret = new HashMap<>();
        ret.put("status", 1);
        ret.put("msg", "请求成功");
        ret.put("data", "id=" + id + ", name=" + name);
        return ret;
    }
}
